import request from "@/api/index";
interface dataSourceType {
  [propName: string]: any;
}
export const  checkForm = async (_rule: dataSourceType = {}, value: any) => {
  if ( _rule.required) {// 必填项验证非空并排除非必须项
    if(!value && value!==0 && value!==false){
      return Promise.reject(`${_rule.msg1}`);
    }
    if(value instanceof Array && !value.length){
      return Promise.reject(`${_rule.msg1}`);
    }
  }
  if (_rule.api && value) { // 没填值没有校验的必要
      var params = detectKey(_rule.key, _rule, value)
      let res = await request[`${_rule.api}`](params);
      if (!res) {
        return Promise.reject(`${_rule.msg2}`);
      } else {
        return Promise.resolve();
      }
  }
  
};
export const detectKey = (key:string, obj:dataSourceType={}, value:any)=>{ // 要检测的key:value
  let params:dataSourceType = {};
  params[key] = value;
  if(key == 'siteDomain' || key == 'siteDomainAlias'){
    params['siteProtocol']=obj.params['siteProtocol']
  }
  return params
}